<template>
  <rj-modal
    :title="$t('groupOperate.devicetitle')"
    :visible="true"
    :maskClosable="false"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-form-model
      :model="form"
      :rules="rules"
      layout="horizontal"
      ref="ruleForm"
      :labelCol="{ span: 10 }"
      :wrapperCol="{ span: 14 }"
    >
      <a-form-model-item :label="$t('groupOperate.tiptitle')" prop="name">
        <a-input :placeholder="$t('groupOperate.detail')" v-model="form.name" />
      </a-form-model-item>
    </a-form-model>
  </rj-modal>
</template>

<script>
import { RjModal } from '@components';
import { maccApi, requestHttp, ValiDateTools } from '@utils/index';
export default {
  name: 'adddeviceGroup',
  props: {
    parentGroup: {
      type: Number,
      require: true,
    },
  },
  data() {
    const self = this;
    const checkName = (rule, value, callback) => {
      const res = ValiDateTools.checkGroupName(value, self.isVip);
      if (res.result) {
        callback();
      } else {
        callback(new Error(res.msg));
      }
    };
    return {
      form: {
        name: '',
      },
      rules: {
        name: [
          {
            required: true,
            validator: checkName,
            trigger: 'blur',
          },
        ],
      },
    };
  },
  components: {
    'rj-modal': RjModal,
  },
  computed: {
    isVip() {
      const userLabel =
        this.$store.state.User?.userInfo?.label?.split(';') ?? [];
      return userLabel.includes('Vip');
    },
  },
  methods: {
    handleOk() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          console.log(valid);
          const { code, msg } = await requestHttp({
            api: `${maccApi.ADD_GROUP_API}`,
            method: 'POST',
            params: {
              pGroupId: this.parentGroup,
              name: this.form.name,
              type: 'DEVICE',
            },
          });
          if (code == 0) {
            this.$emit('closeCallback', { success: true });
          } else {
            this.$message.error(msg || this.$t('groupOperate.addfile'));
          }
        } else {
          return false;
        }
      });
    },
    handleCancel() {
      this.$emit('closeCallback');
    },
  },
};
</script>

<style></style>
